﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading;
using System.Web;
using Top.Api.Util;

namespace TB_WS.Class
{
	public class FireWall_Trade
	{
        public static int Start_FireWall_Created_Trade()
        {
            string Last_Date_Time = Get_Last_Date_Time("Created");
            Class.Unit.INSERT_LOG("获取到最后一次时间：" + Last_Date_Time);
            DataSet New_Create_DST = JST_Trade.Get_Tid_List_Create_Desc(Last_Date_Time);
            Class.Unit.INSERT_LOG("获取到" + New_Create_DST.Tables[0].Rows.Count.ToString() + "条最新订单数据");
            for(int i=0;i<New_Create_DST.Tables[0].Rows.Count;i++)
            {
                //进入到订单处理循环
                string BuyerNick = New_Create_DST.Tables[0].Rows[i][4].ToString();
                string SellerNick = New_Create_DST.Tables[0].Rows[i][3].ToString();
                string Tid= New_Create_DST.Tables[0].Rows[i][0].ToString();
                DataSet Trade_DST = JST_Trade.Search_Trade(BuyerNick);
                if(Trade_DST.Tables[0].Rows.Count>1)
                {
                    //也许存在别家的订单
                    DataSet Not_Seller_Trade_DST = JST_Trade.Search_Trade_Not_Seller(BuyerNick, SellerNick);
                    if(Not_Seller_Trade_DST.Tables[0].Rows.Count==0)
                    {
                        //都是自己家订单 在别家没订单
                        
                    }
                    else
                    {
                        DataSet Push_User_URL_DST = Get_Push_URL_Create(SellerNick);
                        if (Push_User_URL_DST.Tables[0].Rows.Count == 0)
                        {
                            //没有添加过的商家，无视吧；
                        }
                        else
                        {
                            //在别家有订单
                            string Tid_Info_List = "";
                            for (int j = 0; j < Not_Seller_Trade_DST.Tables[0].Rows.Count; j++)
                            {
                                Tid_Info_List = Tid_Info_List +
                                    "\n\n第" + (j + 1) + "条\n订单下单时间：" +
                                    Not_Seller_Trade_DST.Tables[0].Rows[j][5].ToString() + "\n下单卖家：" +
                                    Not_Seller_Trade_DST.Tables[0].Rows[j][3].ToString() + "\n当前订单状态：" +LeShiTao_Library.TB_Top.TB_Words_Info.Exchange_Trade_Status(
                                    Not_Seller_Trade_DST.Tables[0].Rows[j][1].ToString()) + "\n最后一次订单修改时间：" +
                                    Not_Seller_Trade_DST.Tables[0].Rows[j][6].ToString();
                            }
                            string RT_String = "【预警】\n买家ID：" + BuyerNick + "\n订单号：" + Tid + "\n\n" +
                                "有在同行业中下过订单的记录，目前已找到" + Not_Seller_Trade_DST.Tables[0].Rows.Count.ToString() + "条记录\n\n分别为：" + Tid_Info_List;
                            Class.Unit.INSERT_LOG("【记录Log】"+RT_String);
                            //发送推送
                            LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message(Push_User_URL_DST.Tables[0].Rows[0][0].ToString(), RT_String);
                        }
                    }
                }
                else
                {
                    //仅有当前订单
                }
            }
            return 0;
        }

        public static int Start_FireWall_Rate_Trade()
        {
            DataSet Trade_DST = Class.JST_Trade.Get_Last_RD_Data();
            for (int i = 0; i < Trade_DST.Tables[0].Rows.Count; i++)
            {
                if (Trade_DST.Tables[0].Rows[i][1].ToString() != "TRADE_CLOSED_BY_TAOBAO" &&
                    Trade_DST.Tables[0].Rows[i][1].ToString() != "TRADE_FINISHED" &&
                    Trade_DST.Tables[0].Rows[i][1].ToString() != "TRADE_CLOSED")
                {
                    //过滤掉以上三种类型的订单的状态
                    string Trade_JSON = Trade_DST.Tables[0].Rows[i][8].ToString();
                    Thread trade = new Thread(new ParameterizedThreadStart(Create_WP_HB));
                    trade.Start(Trade_JSON);
                }
            }
            return 0;
        }

        public static void Create_WP_HB(object Trade_JSON)
        {
            try
            {
                Top.Api.Response.TradeFullinfoGetResponse rsp = TopUtils.ParseResponse<Top.Api.Response.TradeFullinfoGetResponse>(Trade_JSON.ToString());
                //开始处理订单
                string Buyer_Nick = rsp.Trade.BuyerNick.ToString();
                string Seller_Nick = rsp.Trade.SellerNick.ToString();
                string Tid = rsp.Trade.Tid.ToString();
                int Seller_ID = Convert.ToInt32(Class.Seller_Manage.select_Seller_Info(Seller_Nick).Tables[0].Rows[0][0].ToString());

                DataSet RATE_DST = Class.Rate_Control.Get_Rate_Use_Buyer_Nick(Buyer_Nick);
                int Rate_Buyer_Count = RATE_DST.Tables[0].Rows.Count;
                if (Rate_Buyer_Count == 0)
                {
                    //列表中没有他
                }
                else
                {
                    //列表中存在这个ID 根据权限处理
                    if (GET_FW_PUSHED(Tid) == 1)
                    {
                        //已经存在 不推送
                    }
                    else
                    {
                        //不存在 推送
                        string Push_Strig = "【评价预警订单】\n\n订单号：" + Tid +
                            "\n下单店铺：" + rsp.Trade.SellerNick.ToString() +
                            "\n买家昵称：" + Buyer_Nick.ToString() +
                            "\n在本店下单时间：" + rsp.Trade.Created.ToString() +
                            "\n给过同行中差评数：" + RATE_DST.Tables[0].Rows.Count.ToString() +
                            "\n给同行评价类型：" + RATE_DST.Tables[0].Rows[0][3].ToString() +
                            "\n\n内容：\n" + RATE_DST.Tables[0].Rows[0][4].ToString() +
                            "\n\n在友商店铺给出中差评时间：" + RATE_DST.Tables[0].Rows[0][6].ToString();

                        LeShiTao_Library.DingTalk.Query_Message.Send_Roboot_Message(
                            Get_Push_URL_Modified(Seller_Nick).Tables[0].Rows[0][0].ToString(), Push_Strig);
                    } 
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        public static int GET_FW_PUSHED(string Tid)
        {
            int PD_count = Class.Rate_Control.Get_FW_Tid_Pushed(Tid);
            if (PD_count > 0)
            {
                //已经存在
                return 1;
            }
            else
            {
                Class.Rate_Control.Insert_FW_Tid(Tid);
                return 0;
            }
        }
        public static DataSet Get_Push_URL_Create(string SellerNick)
        {
            string sql = "select robot_url from DingTalk_Robot,Seller_Info,Robot_Type where Seller_Info.id=DingTalk_Robot.seller_ID and Type_ID=Robot_Type.id and Robot_Type.Type_Name='同行下单提醒' and Seller_Info.Seller_Nick='" + SellerNick + "'";
            return Class.Unit.db_Conn.select(sql);
        }

        public static DataSet Get_Push_URL_Modified(string SellerNick)
        {
            string sql = "select robot_url from DingTalk_Robot,Seller_Info,Robot_Type where Seller_Info.id=DingTalk_Robot.seller_ID and Type_ID=Robot_Type.id and Robot_Type.Type_Name='已有中差评预警' and Seller_Info.Seller_Nick='" + SellerNick + "'";
            return Class.Unit.db_Conn.select(sql);
        }
        public static string Get_Last_Date_Time(string Date_Type)
        {
            string sql = "select Date_Time from RDS_DateTime_List where Date_Type='" + Date_Type + "' order by id desc";
            return Class.Unit.db_Conn.scalar(sql);
        }

        
	}
}